package com.android.play;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private final String TAG="MainActivity";
    private List<String> url;
    private ImageSliding mImageSliding;
    private Thread timeThread = null;
    private boolean isExit = false;
    public boolean timeFlag = true;
    public ImageTimerTask timeTaks = null;
    private int ImagePosition=0;
    private ImageAdapter adapter;
    public HashMap<String,Bitmap> imagesCache=new HashMap<String, Bitmap>();//图片缓存
    private String [] imageUrl={
            /*"http://news.xinhuanet.com/photo/2012-02/09/122675973_51n.jpg",
            "http://content.52pk.com/files/100623/2230_102437_1_lit.jpg",
            "http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg",
            "http://img.quwenjiemi.com/2014/0701/thumb_420_234_20140701112917406.jpg"*/};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        onStartTime();


    }

    /**
     * 初始化控件
     */
    private void init(){
        //默认缓存图片
        Bitmap image= BitmapFactory.decodeResource(getResources(),R.mipmap.img_1);
        imagesCache.put("load",image);  //设置缓存中默认的图片
        mImageSliding= (ImageSliding) findViewById(R.id.image_view);
        mImageSliding.setActivity(this);
        url=new ArrayList<String>();
        for (int i = 0 ; i < imageUrl.length ; i++){
            url.add(imageUrl[i]);
        }
        if (url.size()==0){
            adapter=new ImageAdapter(this);
        }else{
            adapter=new ImageAdapter(url,this);
        }
        mImageSliding.setAdapter(adapter);
    }

    /**
     *  autoImageHandler
     */
    Handler autoImageHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    mImageSliding.setSelection(msg.getData().getInt("data"));
                    break;
            }
        }
    };

    Timer autoImage = new Timer();

    /**
     * 开启定时器
     */
    public void onStartTime(){
        timeTaks=new ImageTimerTask();
        autoImage.scheduleAtFixedRate(timeTaks, 3000, 3000);
        timeThread = new Thread() {
            public void run() {
                while(!isExit) {
                    try {
                        Thread.sleep(1500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    synchronized (timeTaks) {
                        if(!timeFlag){
                            timeTaks.timeCondition = true;
                            timeTaks.notifyAll();
                        }
                    }
                    timeFlag = true;
                }
            };
        };
        timeThread.start();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        timeFlag = false;
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        timeTaks.timeCondition = false;
    }

    /**
     *  定时器操作
     */
    class ImageTimerTask extends TimerTask{
        public volatile boolean timeCondition = true;
        @Override
        public void run() {
            synchronized (this) {
                while(!timeCondition) {
                    try {
                        Thread.sleep(100);
                        wait();
                    } catch (InterruptedException e) {
                        Thread.interrupted();
                    }
                }
            }
            try {
                ImagePosition = mImageSliding.getSelectedItemPosition() + 1;
                System.out.println(ImagePosition+"");
                Message msg = new Message();
                Bundle date = new Bundle();// 存放数据
                date.putInt("data", ImagePosition);
                msg.setData(date);
                msg.what = 1;//消息标识
                autoImageHandler.sendMessage(msg);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
